package wiki.kaizen.cloud.security.session.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import wiki.kaizen.cloud.security.session.util.ISecurityUtil;

import java.util.List;

@Getter
@Setter
@ConfigurationProperties(prefix = "kaizen")
public class SecurityProperties {

    /**
     * 全局session过期时间 默认一天
     * */
    private Long globalSessionTimeout = 86400000L;//
    /**
     * session 有效性验证时间间隔
     * */
    private Long sessionValidationInterval = 1800000L;//

    /**
     * 是否开启鉴权缓存
     * */
    private Boolean enableCacheAuth= Boolean.FALSE;

    private AccessAccount account;
    private SecurityFilter securityFilter;
    private Redis redis;
    private Cookie remember;
    private Cookie sessionid;

    @Setter
    @Getter
    public static class Cookie{
        /**
         * cookie 名称
         * */
        private String name ="sid";
        /**
         * cookie 有效期 单位秒 默认十小时
         * */
        private Integer maxAge = 36000;
        /**
         * 设置 js 无法获取cookie
         * */
        private Boolean httpOnly=Boolean.TRUE;
        /**
        * cookie 保存路径
        * */
        private String path ="/";

        /**
         * cookie 作用域
         * */
        private String domain;

    }

    @Setter
    @Getter
    public static class Redis{
        /**
        * 激活的连接最少个数
        * */
        private Integer minIdle = 3;
        /**
        * 激活链接最多个数
        * */
        private Integer maxIdle = 20;
        /**
         * 链接等待时间
         * */
        private Long maxWaitMillis = 1000L;
        /**
         * 连接池数量
         * */
        private Integer maxTotal = 100;

        /**
        * 主机名
        * */
        private String host = "127.0.0.1";
        /**
        * 端口
        * */
        private Integer port = 6379;
        /**
         * 密码
         * */
        private String password="123456";

        /**
         * 超时时间
         * */
        private Integer timeout = 10000;

        /**
        * 连接池
        * */
        private  Short database = 0;
    }



    @Getter
    @Setter
    public static class AccessAccount{


        /**
         * 同一账号最多同时可登录数量
         * */
        private Integer max = 2;
        /**
         * 是否踢出后来登录的账号
         * */
        private Boolean kickoutAfter = Boolean.FALSE;
        /**
        * 提出后转到的地址
        * */
        private String kickoutUrl = "/kaizen/kickout";
        /**
         * 踢出设置的session attribute 的name
         * */
        private String kickoutName = "kickout" ;

        /**
         * 密码试错次数
         * */
        private Short tryMax = 5;

        /*
        * 加密算法
        * */
        private String hashAlgorithmName = "MD5";


        public void setHashAlgorithmName(String hashAlgorithmName) {
            this.hashAlgorithmName = hashAlgorithmName;
            ISecurityUtil.hashAlgorithmName = hashAlgorithmName;
        }

        /**
         *循环次数
         */
        private Short hashIterations = 4;

        public void setHashIterations(Short hashIterations) {
            this.hashIterations = hashIterations;
            ISecurityUtil.hashIterations = hashIterations;
        }

    }

    @Getter
    @Setter
    public static class SecurityFilter{
        private String loginUrl="/kaizen/login";
        private String unauthorizedUrl="/kaizen/unauthorized";
        private String lockedUrl = "/kaizen/locked";
        private List<String> anons;
        private List<String> authcs;
    }
}
